package com.lecyon.farm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lecyon.farm.base.BaseResponse;
import com.lecyon.farm.common.constant.CommonConstant;
import com.lecyon.farm.common.constant.FarmSysConstant;
import com.lecyon.farm.common.exception.FarmException;
import com.lecyon.farm.entity.JsSysOffice;
import com.lecyon.farm.mapper.JsSysOfficeMapper;
import com.lecyon.farm.service.IJsSysOfficeService;
import com.lecyon.farm.util.RedisUtils;
import com.lecyon.farm.util.ResultUtil;
import com.lecyon.farm.util.StringHelper;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * <p>
 * 组织机构表 服务实现类
 * </p>
 *
 * @author YaoZheng
 * @since 2020-04-02
 */
@Service
@Validated
public class JsSysOfficeServiceImpl extends ServiceImpl<JsSysOfficeMapper, JsSysOffice> implements IJsSysOfficeService {

    private RedisUtils redisUtils;


    /**
     * 根据officeCode 查找 jsSysOffice
     *
     * @param officeCode
     * @return
     */
    @Override
    public JsSysOffice getByOfficeCode(String officeCode) {
        JsSysOffice result;
        if (StringHelper.isEmpty(officeCode)) {
            return null;
        }
        boolean flag;
        flag = redisUtils.hHasKey(FarmSysConstant.REDIS_KEY_JS_SYS_OFFICE, officeCode);
        if (flag) {
            String jsonStr = (String) redisUtils.hGet(FarmSysConstant.REDIS_KEY_JS_SYS_OFFICE, officeCode);
            result = JSON.toJavaObject(JSONObject.parseObject(jsonStr), JsSysOffice.class);
        } else {
            QueryWrapper<JsSysOffice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("office_code", officeCode);
            int count = count(queryWrapper);
            if (1 != count) {
                return null;
            } else {
                result = getOne(queryWrapper);
            }
        }
        return result;
    }

    @Override
    public List<JsSysOffice> getAll() {
        List<JsSysOffice> result;
        boolean flag;
        flag = redisUtils.hasKey(FarmSysConstant.REDIS_KEY_JS_SYS_OFFICE_ALL_DATA);
        if (flag) {
            String jsonStr = (String) redisUtils.get(FarmSysConstant.REDIS_KEY_JS_SYS_OFFICE_ALL_DATA);
            result = JSON.parseArray(jsonStr,JsSysOffice.class);
        } else {
            result = list();
        }
        return result;
    }

    /**
     * 将系统现有的区域数据放入redis中
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostConstruct
    public BaseResponse statisticJsSysOffice() {
//        QueryWrapper<JsSysOffice> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lt("office_type",4);
//        List<JsSysOffice> resultList = list(queryWrapper);
        List<JsSysOffice> resultList = list();
        if (CollectionUtils.isNotEmpty(resultList)) {
            boolean flag;
            String itemKey;
            for (JsSysOffice jsSysOffice : resultList) {
                itemKey = jsSysOffice.getOfficeCode();
                flag = redisUtils.hSet(FarmSysConstant.REDIS_KEY_JS_SYS_OFFICE, itemKey, JSON.toJSONString(jsSysOffice));
                if (!flag) {
                    throw new FarmException(CommonConstant.EX_DB_REDIS_ERROR, "系统缓存数据库异常");
                }
            }
            flag = redisUtils.set(FarmSysConstant.REDIS_KEY_JS_SYS_OFFICE_ALL_DATA, JSON.toJSONString(resultList));
            if (!flag) {
                throw new FarmException(CommonConstant.EX_DB_REDIS_ERROR, "系统缓存数据库异常");
            }
        }
        return ResultUtil.success();
    }

    /**
     * ----------------------------- 分割线 以下是setter方法，用于替换field上的@Autowired -----------------------------
     **/

    @Autowired
    @Lazy
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }
}
